package com.dzl.goods.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.goods.dao.WelfareGoodsAttrMapper;
import com.dzl.goods.pojo.WelfareGoodsAttr;
import com.dzl.goods.pojo.vo.WelfareSkuGoodsVo;
import com.dzl.goods.pojo.vo.WelfareGoodsAttrVoucherVo;
import com.dzl.goods.service.WelfareGoodsAttrService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 商品sku规格表(WelfareGoodsAttr)表服务实现类
 * @author ChenLong
 * @since 2021-05-18 17:58:36
 */
@Service("welfareGoodsAttrService")
public class WelfareGoodsAttrServiceImpl extends BaseServiceImpl<WelfareGoodsAttr> implements WelfareGoodsAttrService {
    @Autowired
    private WelfareGoodsAttrMapper welfareGoodsAttrMapper;

    @Override
    public List<Integer> getGoodsAttrSkuListBySkuId(List<Integer> skuIdList, Integer goodsId) {
        return welfareGoodsAttrMapper.getGoodsAttrSkuListBySkuId(skuIdList, goodsId);
    }

    @Override
    public Integer deleteLogicGoodsAttrSkuListBySkuId(List<Integer> skuIdList) {
        return welfareGoodsAttrMapper.deleteLogicGoodsAttrSkuListBySkuId(skuIdList);
    }

    @Override
    public Integer deleteGoodsAttrSkuListBySkuId(List<Integer> skuIdList) {
        return welfareGoodsAttrMapper.deleteGoodsAttrSkuListBySkuId(skuIdList);
    }

    @Override
    public Integer insertGoodsAttrSkuList(List<WelfareGoodsAttr> list) {
        return welfareGoodsAttrMapper.insertGoodsAttrSkuList(list);
    }

    @Override
    public List<WelfareGoodsAttrVoucherVo> getGoodsAttrWithVoucherByGoodsId(Integer goodsId) {
        return welfareGoodsAttrMapper.getGoodsAttrWithVoucherByGoodsId(goodsId);
    }

    @Override
    public WelfareGoodsAttrVoucherVo getGoodsAttrWithVoucher(WelfareGoodsAttr welfareGoodsAttr) {
        return welfareGoodsAttrMapper.getGoodsAttrWithVoucher(welfareGoodsAttr);
    }

    @Override
    public List<WelfareSkuGoodsVo> getGoodsAttrsByGoodsId(String ids) {
        List<WelfareSkuGoodsVo> goodsAttrVos = welfareGoodsAttrMapper.getGoodsAttrsByGoodsId(ids);
        if (EmptyUtils.isNotEmpty(goodsAttrVos)){
            goodsAttrVos.forEach(vo ->{
                StringBuilder str = new StringBuilder();
                if (EmptyUtils.isNotEmpty(vo.getFirstDetailName())) {
                    str.append(vo.getFirstDetailName());
                }
                if (EmptyUtils.isNotEmpty(vo.getSecondDetailName())) {
                    if (EmptyUtils.isNotEmpty(vo.getFirstDetailName())) {
                        str.append("/");
                    }
                    str.append(vo.getSecondDetailName());
                }
                if (EmptyUtils.isNotEmpty(vo.getThirdDetailName())) {
                    if (EmptyUtils.isNotEmpty(vo.getFirstDetailName()) || EmptyUtils.isNotEmpty(vo.getSecondDetailName())) {
                        str.append("/");
                    }
                    str.append(vo.getThirdDetailName());
                }
                vo.setGoodsAttr(str.toString());
                //插入图片
                String img = "";
                if (EmptyUtils.isNotEmpty(vo.getSkuPic())){
                    img = vo.getSkuPic();
                }else if (EmptyUtils.isNotEmpty(vo.getGoodsImg())){
                    img = vo.getGoodsImg();
                }
                vo.setGoodsImg(img);
            });
        }
        return goodsAttrVos;
    }

    @Override
    public void export(List<WelfareSkuGoodsVo> voList, HttpServletResponse res) {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle();
        HSSFCellStyle cellStyle1 = hssfWorkbook.createCellStyle();
        //换行
        cellStyle.setWrapText(true);
        HSSFDataFormat format = hssfWorkbook.createDataFormat();
        HSSFSheet sheet = hssfWorkbook.createSheet();
        HSSFFont font = hssfWorkbook.createFont();
        font.setFontName("宋体");
        cellStyle.setFont(font);
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        sheet.setColumnWidth(0, 10 * 255);
        row.createCell(1).setCellValue("主图");
        sheet.setColumnWidth(1, 15 * 255);
        row.createCell(2).setCellValue("商品名称");
        sheet.setColumnWidth(2, 20 * 255);
        row.createCell(3).setCellValue("品牌");
        sheet.setColumnWidth(3, 20 * 255);
        row.createCell(4).setCellValue("规格");
        sheet.setColumnWidth(4, 20 * 255);
        row.createCell(5).setCellValue("销售价");
        sheet.setColumnWidth(5, 20 * 255);
        row.createCell(6).setCellValue("成本价");
        sheet.setColumnWidth(6, 20 * 255);
        if (EmptyUtils.isNotEmpty(voList)){
            for (int i = 0; i < voList.size(); i++) {
                HSSFRow row1 = sheet.createRow(i + 1);
                //行高
                row1.setHeightInPoints((short)80);
                //商品编号
                HSSFCell cell0 = row1.createCell(0);
                //水平居中且垂直居中
                cellStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
                cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                cell0.setCellValue(i+1);
                cell0.setCellStyle(cellStyle1);
                //主图
                HSSFCell cell1 = row1.createCell(1);
                //插入图片
                String img = "";
                if (EmptyUtils.isNotEmpty(voList.get(i).getSkuPic())){
                    img = voList.get(i).getSkuPic();
                }else if (EmptyUtils.isNotEmpty(voList.get(i).getGoodsImg())){
                    img = voList.get(i).getGoodsImg();
                }
                if (EmptyUtils.isNotEmpty(img)){
                    byte[] bytes = ImageUtils.image2Base64(img + "?x-oss-process=image/resize,m_fill,h_300,w_300");
                    buildImgUrl(hssfWorkbook, sheet,bytes, i, cell1);
                }
                //商品名称
                cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
                HSSFCell cell2 = row1.createCell(2);
                cell2.setCellValue(voList.get(i).getGoodsName());
                cell2.setCellStyle(cellStyle);
                //品牌名称
                HSSFCell cell3 = row1.createCell(3);
                cell3.setCellValue(voList.get(i).getBrandName());
                cellStyle.setDataFormat(format.getFormat("@"));
                cell3.setCellStyle(cellStyle);
                //规格
                HSSFCell cell4 = row1.createCell(4);
                cell4.setCellValue(voList.get(i).getGoodsAttr());
                cell4.setCellStyle(cellStyle);
                //销售价
                HSSFCell cell5 = row1.createCell(5);
                cell5.setCellValue(voList.get(i).getPrice().toString());
                cell5.setCellStyle(cellStyle);
                //成本价
                HSSFCell cell6 = row1.createCell(6);
                cell6.setCellValue(voList.get(i).getCostPrice().toString());
                cell6.setCellStyle(cellStyle);
            }
        }
        res.setHeader("Content-Disposition", "attachment;Filename=" + System.currentTimeMillis() + ".xlsx");
        // 响应类型,编码
        res.setContentType("application/octet-stream;charset=UTF-8");
        try {
            // 形成输出流
            OutputStream out = res.getOutputStream();
            res.reset();
            hssfWorkbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void buildImgUrl(HSSFWorkbook workBook,  HSSFSheet sheet,byte[] bytes, int i, HSSFCell imgUrlCell) {
        //画图的顶级管理器，一个sheet只能获取一个（一定要注意这点）
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        //anchor主要用于设置图片的属性
        // HSSFClientAnchor anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2)
        HSSFClientAnchor anchor = new HSSFClientAnchor(1, 1, 10, 255,(short) 1, 1+i, (short) 2, 1+i);
        anchor.setAnchorType(3);
        //插入图片
        imgUrlCell.equals( patriarch.createPicture(anchor, workBook.addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG)));
    }
}
